package com.yxq.mdt.app.modules.personal.ui.activity;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.orhanobut.hawk.Hawk;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.modules.personal.bean.MyPersonalBean;
import com.yxq.mdt.app.modules.personal.bean.PrivateSetResultBean;
import com.yxq.mdt.app.modules.personal.model.SettingModel;
import com.yxq.mdt.app.modules.personal.presenter.ISettingContract;
import com.yxq.mdt.app.modules.personal.presenter.SettingPresenter;

import butterknife.BindView;

/**
 * @author L
 * @time
 * @describe 隐私设置
 */
public class PrivacySettingActivity extends BaseActivity<SettingPresenter> implements ISettingContract.View {


    @BindView(R.id.ps_checkbox_private)
    CheckBox psCheckboxPrivate;
    @BindView(R.id.ps_checkbox_dynamic)
    CheckBox psCheckboxDt;
    @BindView(R.id.ps_checkbox_other)
    CheckBox psCheckboxOther;

    @Override
    public int getLayoutId() {
        return R.layout.activity_privacy_setting;
    }

    @Override
    public void initView() {
        MyPersonalBean bean = Hawk.get(SPKey.SP_KEY_USER_MY_INFO);
        if (bean != null) {
            psCheckboxPrivate.setChecked("1".equals(bean.getShowInfo()));
        }
        //个人隐私
        psCheckboxPrivate.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                switchSettingStatus(compoundButton.getId(), b);
            }
        });
        //动态隐私
        psCheckboxDt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                switchSettingStatus(compoundButton.getId(), b);
            }
        });
        //查看他人资料隐私
        psCheckboxOther.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                switchSettingStatus(compoundButton.getId(), b);
            }
        });
    }

    /**
     * 设置状态
     *
     * @param id
     * @param b
     */
    private void switchSettingStatus(int id, boolean b) {
        switch (id) {
            case R.id.ps_checkbox_private://个人隐私
                mPresenter.updateGaUserExtShowInfo(b ? "1" : "0");
                break;
            case R.id.ps_checkbox_dynamic://动态隐私
                break;
            case R.id.ps_checkbox_other://查看他人资料隐私
                break;
        }
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mPresenter = new SettingPresenter(this, new SettingModel());
    }

    @Override
    public void onLoadSuccess(PrivateSetResultBean data) {
        MyPersonalBean bean = Hawk.get(SPKey.SP_KEY_USER_MY_INFO);
        if (data != null && data.getShowInfo() != null) {
            bean.setShowInfo(data.getShowInfo());
            Hawk.put(SPKey.SP_KEY_USER_MY_INFO, bean);
        }
    }

    @Override
    public void onLoadFailed(String msg) {
        ToastUtils.showShort("修改失败" + msg);
    }
}
